package demo.collection;

import java.util.*;
import java.util.stream.Collectors;

public class Demo1 {
    public static void main1(String[] args) {

        Integer[] arr = new Integer[]{1,2,3,6,4};
        List<Integer> list1 = Arrays.stream(arr).toList();
        // Java 16, Arrays.stream(arr)会创建一个包含数组元素的流, 然后toList()方法将这个流收集到一个不可变列表中
        // list1.add(7);  会报错 UnsupportedOperationException
        List<Integer> list2 = Arrays.stream(arr).collect(Collectors.toList());
        list2.add(8);
        // 使用Collectors.toList() 将流收集到一个可变的列表中, 此时 add 不会报错


        Deque<Integer> queue = new LinkedList<>();
        Deque<Integer> stack = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        System.out.println(queue.peek());
//        stack.push(1);
//        stack.push(2);
//        System.out.println(stack.pop());
//        System.out.println(stack.pop());
//        List<Integer> list = new LinkedList<>();
//        Set<Integer> set = new HashSet<>();
//        set.add(1);
//        set.add(2);
//        List<Integer> list = new ArrayList<>(set);
//        System.out.println(list);
    }

    public static void main2(String[] args) {
        HashSet<Integer> hashSet = new HashSet<>();
        Object[] array = hashSet.toArray();
        hashSet.add(1);
        hashSet.add(2);
        hashSet.add(4);
        hashSet.add(3);
        for (Integer i: hashSet) {
            System.out.println(i);
        }
        TreeSet<Integer> treeSet = new TreeSet<>(Comparator.reverseOrder());

    }

    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "x");
        hashMap.get(1);
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(1, null);
        treeMap.put(2, "0");
        SortedMap<Integer, String> map = new TreeMap<>();
        TreeMap<Integer, String> treeMap1 = new TreeMap<>((x1, x2) -> x2.compareTo(x1));
        TreeMap<Integer, String> treeMap2 = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        System.out.println(treeMap1.comparator());
        System.out.println(treeMap2.comparator());

    }
}
